using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.IO;

namespace KBMVC.Controllers
{
    public class ArticleController : Controller
    {
        //
        // GET: /Article/

        public ActionResult Index()
        {
            if (RouteData.Values["id"].ToString().Trim().Length > 0)
            {
                KBMVC.Core.Article article =
                    new KBMVC.Core.Article(
                        RouteData.Values["id"].ToString().Trim()
                        );

                //decode information pulled from XML file - temporary fix while using LINQ2XML
                StringWriter myWriter = new StringWriter();
                HttpUtility.HtmlDecode(article.Content, myWriter);
                article.Content = myWriter.ToString();

                return View(article);
            }

            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit()
        {
            return View();
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(KBMVC.Core.Article article)
        {
            if (ModelState.IsValid)
            {
                //encode information in content box from XML file - temporary fix while using LINQ2XML
                article.Content = Server.HtmlEncode(article.Content);

                article.Save();

                return View("Index", article);
            }
            else
                return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete()
        {
            ViewData["ItemID"] = RouteData.Values["id"];
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(string id)
        {
            KBMVC.Core.Article article = new KBMVC.Core.Article(id);

            article.Delete();

            return View("Index");
        }

    }
}
